package com.ship.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ship.commens.Result;
import com.ship.dto.DeptDto;
import com.ship.dto.DeptSearchDto;
import com.ship.mapper.UserMapper;
import com.ship.model.Dept;
import com.ship.service.DeptService;
import com.ship.mapper.DeptMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Lucky Boy
* @description 针对表【dept】的数据库操作Service实现
* @createDate 2024-07-01 15:56:25
*/
@Service
public class DeptServiceImpl implements DeptService{


    @Resource
    private DeptMapper deptMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public Result findAllDepts() {

        List<Dept> deptList = deptMapper.selectAll();
        Result result = Result.SUCCESS;
        result.setData(deptList);
        return result;
    }

    @Override
    public Result findDeptsByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Dept> depts = deptMapper.selectAll();
        PageInfo<Dept> deptPageInfo = new PageInfo<>(depts);
        Result result = Result.SUCCESS;
        result.setData(deptPageInfo.getList());
        result.setTotal(deptPageInfo.getTotal());
        return result;
    }

    @Override
    public Result saveDepts(DeptDto deptDto) {
        int rows = deptMapper.insertDepts(deptDto);
        if (rows<1){
            return new Result(-1,"部门添加失败");
        }
        return Result.SUCCESS;
    }

    @Override
    public Result removeDeptById(Long deptId) {

        int rows = deptMapper.deleteOne(deptId);
        if (rows < 1){
            return new Result(-1, "删除部门失败！");
        }

        //删除与部门有关的人员的部门Id
        rows = userMapper.updateUserDept_idNull(deptId);
        Result result = new Result();
        return result;
    }

    @Override
    public Result removeMany(String deptIds) {
        int rows = deptMapper.deleteMany(deptIds);
        if (rows<1){
            return new Result(-1,"批量删除失败");
        }
        return Result.SUCCESS;
    }

    @Override
    public Result findDeptsBySearch(DeptSearchDto deptSearchDto) {
        PageHelper.startPage(deptSearchDto.getPageNum(), deptSearchDto.getPageSize());

        List<Dept> deptList = deptMapper.selectDeptsBySearch(deptSearchDto);

        PageInfo<Dept> pageInfo = new PageInfo<>(deptList);
        Result result = new Result();
        result.setData(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    @Override
    public Result modifyDept(DeptDto deptDto) {
        int rows = deptMapper.selectDeptExists(deptDto.getId());
        if(rows < 1){
            return new Result(-1, "部门不存在");
        }

        rows = deptMapper.updateDept(deptDto);
        if(rows < 1){
            return new Result(-1, "修改失败！");
        }
        Result result = new Result();
        return result;
    }
}
